package com.example.myapplication;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

//自定义适配器
public class EBAdapter extends ArrayAdapter<ElectricBusiness> {
    private int resourceId;

    private static class ViewHolder {
        ImageView ebImage;
        TextView ebName;
    }

    public EBAdapter(Context context, int textViewResourceId, List<ElectricBusiness> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ElectricBusiness eb = getItem(position);
        ViewHolder viewHolder;
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.ebImage = view.findViewById(R.id.iv_eb);
            viewHolder.ebName = view.findViewById(R.id.tv_eb);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.ebImage.setImageResource(eb.getIcon());
        viewHolder.ebName.setText(eb.getName());

        return view;
    }
}